﻿using Estsh.Core.Model.EnumUtil;
using Estsh.Core.Models;
using Estsh.Core.Quartz.BaseService;
using Estsh.Core.Util;
using Estsh.Core.Wms.IRepositories;

namespace Estsh.Core.Wms.Jobs
{
    /// <summary>
    /// 大件物料拉动根据生产JIT信息校对Kitting区库存是否足够，当库存不足时，
    /// 自动产生生产领料单
    /// </summary>
    public class EarlyJitPullRevisionJob : IJobService
    {
        private readonly IPullingRepository pullRepository;
        private readonly IMoveOrderRepository moveOrderRepository;
        private readonly IStockRepository stockRepository;



        public EarlyJitPullRevisionJob(IPullingRepository _pullGroupRepository,
            IMoveOrderRepository _moveOrderRepository,
            IStockRepository _stockRepository)
        {
            this.pullRepository = _pullGroupRepository;
            this.moveOrderRepository = _moveOrderRepository;
            this.stockRepository = _stockRepository;

        }

        public string ExecuteService(string parameter)
        {
            try
            {
                List<WmsMoveDetail> pullDetails = pullRepository.GetPullingPartList();
                if (pullDetails.Count <= 0)
                {
                    return "没有待拉动数据！";
                }

                List<int> pdlineIds = pullDetails.Select(a => a.PdlineId).Distinct().ToList();
                List<string> orderNos = pullDetails.Select(a => a.OrderNo).Distinct().ToList();

                foreach (var pdlineId in pdlineIds)
                {
                    var details = pullDetails.Where(a => a.PdlineId == pdlineId).ToList();
                    var pdlineZone = pullRepository.GetPdlineZone(pdlineId);

                    List<WmsMoveDetail> sumDetails = new List<WmsMoveDetail>();
                    details = details.OrderBy(a=>a.PartId).ToList();
                    int partId = 0;
                    foreach (var item in details)
                    {
                        if (partId == item.PartId)
                        {
                            var tempItem = sumDetails.FirstOrDefault(a=>a.PartId == partId);
                            tempItem.Qty += item.Qty;
                        }
                        else
                        {
                            sumDetails.Add(item);
                            partId = item.PartId;
                        }
                    }

                    List<WmsMoveDetail> sumDetails111 = new List<WmsMoveDetail>();
                    details.GroupBy(a => a.PartId, (a, b) =>
                    {
                        var total = b.Sum(x => x.Qty);
                        var tt = b.Select(y =>
                        {
                            y.Qty = total;
                            return y;
                        }).ToList();
                        sumDetails111.Add(tt.First());
                        return tt;
                    }).ToList();

                    List<WmsMoveDetail> newDetails = new List<WmsMoveDetail>();
                    details.GroupBy(a => a.PartId, (a, b) =>
                    {
                        var total = b.Sum(x => x.Qty);

                        var stockQty = stockRepository.GetStockQty(a, pdlineZone.ZoneId);
                        var pickQty = moveOrderRepository.GetPickQty(a, pdlineZone.ZoneId);
                        var tt = b.First();
                        if (total > stockQty + pickQty)
                        {
                            tt.Qty = total - stockQty - pickQty;
                            newDetails.Add(tt);
                        }
                        return tt;
                    }).ToList();

                    if (newDetails.Count <= 0)
                    {
                        continue;
                    }

                    moveOrderRepository.CreatePullOrder("0", newDetails,pdlineZone);
                }

                foreach (var orderNo in orderNos)
                {
                    pullRepository.UpdateWorkOrder(orderNo);
                }

                return "处理完成！";

            }
            catch (Exception ex) 
            {
                LogHelper.Error("处理异常！" + ex);
                return "处理异常！" + ex.Message;
            }
            
        }
    }
}
